package module.ahleteView;

import bean.Athlete;
import bean.Competition;
import bean.EntryForm;
import dao.EntryFromDao;
import dao.impl.CompetitionDaoImpl;
import dao.impl.EntryFormDaoImpl;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @Auther leiHua
 * @Date 2022/5/22 17:44
 */
public class AthleteMyCompetition extends JPanel {
    //创建的表格宽度
    int tableWidth = 1000;
    //创建的表格高度
    int tableHeight = 650;
    JTable jTable;
    JButton baoming;
    JButton certain;

    public AthleteMyCompetition() {

        this.setSize(1000, 1000);
        this.setLayout(null);

        JScrollPane jScrollPane = new JScrollPane(); //滚动窗口
        jScrollPane.setSize(tableWidth, tableHeight);
        jScrollPane.setBounds(0, 0, tableWidth, tableHeight);

        jTable = new JTable(0, 4);

        jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
        //设置行高
        jTable.setRowHeight(30);
        //设置列名
        Object[] header = {"比赛名称", "比赛地点", "比赛时间","审批状态"};
        DefaultTableModel model = (DefaultTableModel) jTable.getModel();
        model.setColumnIdentifiers(header); //设置列标识符 （列名）

        //列名字体设置
        jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));

        jTable.getTableHeader().setBackground(new Color(211, 237, 215));
//        model.addRow(header);
//-----------------------------------------------------------------------------------------
        //去报名表查询当前运动员报了什么项目
        EntryFormDaoImpl entryFromDao = new EntryFormDaoImpl();
        ArrayList<String> c_idList = entryFromDao.selectBaseAthlete(Athlete.myA_id);  //查询到该运动员所有已经报名的项目ID
        //遍历项目ID 添加到表格中
        if (c_idList.size() != 0) {
            CompetitionDaoImpl competitionDao = new CompetitionDaoImpl();
            EntryFormDaoImpl entryFormDao = new EntryFormDaoImpl();
            for (String id : c_idList) {
                Competition competition = competitionDao.select(id);
                EntryForm entryForm = entryFormDao.selectBaseCompetition(id);
                String competitionName = competition.getCompetitionName();
                String palce = competition.getPalce();
                String time = competition.getTime();
                String status=entryForm.getStatus();
                Vector vector = new Vector();
                vector.add(competitionName);
                vector.add(palce);
                vector.add(time);
                vector.add(status);
                model.addRow(vector);
            }
        } else {
            JLabel label = new JLabel("您还没有参加项目哦，快去报名参加比赛吧");
            label.setFont(new Font("宋体", Font.TYPE1_FONT, 30));
            ImageIcon imageIcon = new ImageIcon("src/imag/logo.jpg");
            imageIcon.setImage(imageIcon.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
            JOptionPane.showMessageDialog(null, label, "未参加比赛", JOptionPane.INFORMATION_MESSAGE, imageIcon);
        }


        //设置表中数据文字居中显示(这段是网上copy找的)
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        dc.setHorizontalAlignment(JLabel.CENTER);
        jTable.setDefaultRenderer(Object.class, dc);
        //设置表头 文字居中
        DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
        hr.setHorizontalAlignment(JLabel.CENTER);
        jTable.getTableHeader().setDefaultRenderer(hr);


        jTable.setModel(model);//设置成动态表格
        //将视图设为表格setViewportView
        //jScrollPane.add(jTable); (不行)
        jScrollPane.setViewportView(jTable);
        this.add(jScrollPane);
        jScrollPane.setAutoscrolls(true);


    }

}
